﻿Public Class FrmLichSuMuonTraHSDV

    Private Sub FrmLichSuMuonTraHSDV_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadHuyen(cmbHuyen)
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
        LoadPhong(cmbPhong)
        LoadKe(cmbKe)
        LoadKho(cmbKho)
        LoadTinhTrang(cmbTinhTrang)
        NapDSCongTrinh()

        dgvDSCongTrinh.RowsDefaultCellStyle.BackColor = Color.Beige
        dgvDSCongTrinh.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure

        dgvLSMuonTraHoSo.RowsDefaultCellStyle.BackColor = Color.Beige
        dgvLSMuonTraHoSo.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub

    Public Sub NapDSCongTrinh()
        Dim sql As String
        sql = "SELECT CONGTRINH.SoHoSo, CONGTRINH.TenCongTrinh,  CONVERT(CHAR(10), CONGTRINH.NgayNhan, 103) AS NgayNhan1, CONGTRINH.NgayNhan, CONGTRINH.HoSoNam, CONGTRINH.ThanhPhanHoSo, " &
              "TENHUYEN.T_Huyen, TENXA.TenXa, DMPhong.TenPhong, DMKe.TenKe, DMKho.TenKho, DMTinhTrang.TenTinhTrang " &
              "FROM CONGTRINH LEFT OUTER JOIN " &
              "DMPhong ON congtrinh.MaPhongID = DMPhong.MaPhongID LEFT OUTER JOIN " &
              "DMKe ON CONGTRINH.MaKeID = DMKe.MaKeID LEFT OUTER JOIN " &
              "DMKho ON CONGTRINH.MaKhoID = DMKho.maKhoID LEFT OUTER JOIN " &
              "DMTinhTrang ON CONGTRINH.MaTinhTrangID = DMTinhTrang.MaTinhTrangID LEFT OUTER JOIN " &
              "TENXA ON CONGTRINH.MaXa = TENXA.MaXa LEFT OUTER JOIN " &
              "TENHUYEN ON TENHUYEN.maH = TENXA.MaHuyen " &
              "WHERE CONGTRINH.TENCONGTRINH like N'%" & txtTenCongTrinh.Text.Trim() & "%'"
        dgvDSCongTrinh.DataSource = LoadThongTin(sql)
        dgvDSCongTrinh.Refresh()
    End Sub
    Public Sub LoadLichSuMuonTra()
        Dim sql As String
        sql = "SELECT CONVERT(CHAR(10),PHIEUMUON_HSDV.NgayMuon,103) AS NGAYMUON, PHIEUMUON_HSDV.NguoiMuon, PHIEUMUON_HSDV.MucDichMuon, Login.hotenNguoiDangNhap as NguoiChoMuon, " &
              "CONVERT(CHAR(10),PHIEUMUON_HSDV.NgayTra,103) AS NGAYTRA,Login1.hotenNguoiDangNhap as NguoiNhanTra " &
              "FROM PHIEUMUON_HSDV LEFT OUTER JOIN " &
                   "QL_MuonHoSoDichVu ON PHIEUMUON_HSDV.SoPhieuMuon = QL_MuonHoSoDichVu.SoPhieuMuon LEFT OUTER JOIN " &
                   "Login ON QL_MuonHoSoDichVu.NguoiChoMuon = Login.TenDangNhap LEFT OUTER JOIN " &
                   "QL_TraHoSoDichVu ON PHIEUMUON_HSDV.SoPhieuMuon = QL_TraHoSoDichVu.SoPhieuMuon LEFT OUTER JOIN " &
                   "Login As Login1 ON QL_TRaHoSoDichVu.NguoiNhanTra = Login1.TenDangNhap " &
              "WHERE(PHIEUMUON_HSDV.SoHoSo = '" & txtSoHoSo.Text & "')"

        dgvLSMuonTraHoSo.DataSource = LoadThongTin(sql)
        dgvLSMuonTraHoSo.Refresh()

    End Sub

    Private Sub dgvDSCongTrinh_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSCongTrinh.CellClick
        Dim dong As Integer = e.RowIndex

        'Kiem tra phai dong tieu de ko
        If (dong = -1) Then
            Return
        End If

        'Dua du lieu tu DataGridView len cac textbox va combox
        txtSoHoSo.Text = dgvDSCongTrinh.Rows(dong).Cells("SoHoSo").Value.ToString()
        txtTenCongTrinh.Text = dgvDSCongTrinh.Rows(dong).Cells("TenCongTrinh").Value.ToString()

        dtNgayNhan.Text = String.Format("{0:dd/MM/yyyy}", dgvDSCongTrinh.Rows(dong).Cells("NgayNhan").Value.ToString())

        txtHoSoNam.Text = dgvDSCongTrinh.Rows(dong).Cells("HoSoNam").Value.ToString()
        txtThanhPhanHoSo.Text = dgvDSCongTrinh.Rows(dong).Cells("ThanhPhanHoSo").Value.ToString()
        cmbHuyen.Text = dgvDSCongTrinh.Rows(dong).Cells("T_Huyen").Value.ToString()
        cmbXa.Text = dgvDSCongTrinh.Rows(dong).Cells("TenXa").Value.ToString()
        cmbPhong.Text = dgvDSCongTrinh.Rows(dong).Cells("TenPhong").Value.ToString()
        cmbKe.Text = dgvDSCongTrinh.Rows(dong).Cells("TenKe").Value.ToString()
        cmbKho.Text = dgvDSCongTrinh.Rows(dong).Cells("TenKho").Value.ToString()
        cmbTinhTrang.Text = dgvDSCongTrinh.Rows(dong).Cells("TenTinhTrang").Value.ToString()

        LoadLichSuMuonTra()
    End Sub

    Private Sub txtTenCongTrinh_KeyUp(sender As Object, e As KeyEventArgs) Handles txtTenCongTrinh.KeyUp
        NapDSCongTrinh()
    End Sub
    Private Sub dgvLSMuonTraHoSo_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvLSMuonTraHoSo.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < dgvLSMuonTraHoSo.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If dgvLSMuonTraHoSo.RowHeadersWidth < Size.Width + 20 Then dgvLSMuonTraHoSo.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
    Private Sub dgvDSCongTrinh_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvDSCongTrinh.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < dgvDSCongTrinh.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If dgvDSCongTrinh.RowHeadersWidth < Size.Width + 20 Then dgvDSCongTrinh.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub

    Private Sub dgvDSCongTrinh_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSCongTrinh.CellContentClick

    End Sub
End Class